﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Automator.Runtime;
using Automator.Tasks.Core;

namespace Automator.Classes
{
    /// <summary>
    /// Value converter used for Task.ToString() ellipsis to display in a project explorer
    /// </summary>
    public class CommandLineConverter: IValueConverter
    {
        private int length = 50;
        
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string _commandLine = value.ToString();
            return (_commandLine.Length > length)
                ? new StringBuilder(_commandLine.Substring(0, length)).Append("...").ToString()
                : _commandLine;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
   
    /// <summary>
    /// Value converter used to show various images depending on LogMessageType
    /// </summary>
    public class LogImageConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is LogMessageType)
            {
                string uriPath;

                switch ((LogMessageType)value)
                {
                        case LogMessageType.Error: uriPath = "resources/error.png";
                        break;
                        case LogMessageType.Info: uriPath = "Resources/info.png";
                        break;
                        case LogMessageType.Warning: uriPath = "Resources/warning.png";
                        break;
                        case LogMessageType.Application: uriPath = "Resources/application.png";
                        break;
                        case LogMessageType.Ok: uriPath = "Resources/ok.png";
                        break;
                        case LogMessageType.Run: uriPath = "Resources/run.png";
                        break;
                        default: uriPath = "Resources/info.png";
                        break;
                }
                
                string prefix = "/Automator;component/";
                return new BitmapImage(new Uri(String.Concat(prefix, uriPath), UriKind.RelativeOrAbsolute));
            }
            else return null;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Value converter to show DateTime in log frontend
    /// </summary>
    public class LogDateTimeConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Value converter to show current state of Task
    /// </summary>
    public class TaskStateImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is TaskExecutingState)
            {
                string uriPath;

                switch ((TaskExecutingState)value)
                {
                        case TaskExecutingState.Completed: uriPath = "resources/ok.png";
                        break;
                        case TaskExecutingState.Failed: uriPath = "resources/error.png";
                        break;
                        case TaskExecutingState.Executing: uriPath = "resources/run.png";
                        break;
                        default: uriPath = String.Empty;
                        break;
                }

                return new BitmapImage(new Uri(uriPath, UriKind.Relative));
            }
            else return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Value converter to show solution tree images
    /// </summary>
    public class WorkflowImageConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Solution)
                return new BitmapImage(new Uri("/Automator;component/resources/application.png", UriKind.RelativeOrAbsolute));
            else
                return new BitmapImage(new Uri("/Automator;component/resources/workflow.png", UriKind.RelativeOrAbsolute));
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Value converter to show command tooltip
    /// </summary>
    public class TooltipConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Button button = value as Button;
            StringBuilder sb = new StringBuilder();
            if (button != null && button.Command != null)
            {
                RoutedUICommand command = button.Command as RoutedUICommand;
                sb.Append(command.Text);
                if (command.InputGestures.Count > 0)
                    sb.Append(String.Format(" ({0})", (command.InputGestures[0] as KeyGesture).DisplayString));
            }
            return sb.ToString();
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Value converter to show RSS posts at start page
    /// </summary>
    public class RssDescriptionConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string source = value.ToString();
            while (source.Contains("<"))
            {
                int startindex = source.IndexOf("<");
                int endindex = source.IndexOf(">");
                string tag = source.Substring(startindex, endindex - startindex + 1);
                source = source.Replace(tag, String.Empty).Replace("&#8220;", @"""").Replace("&#8221;", @"""");
            }
            return source;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
